package net.pws.oos.web.dto;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.pws.oos.biz.model.Checkin;

public class CheckinEvent {
	
	private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm");

	public static CheckinEvent from(Checkin model) {
		if(model == null) {
			return null;
		}
		CheckinEvent event = new CheckinEvent();
		event.setId(model.getId());
		event.setUrl("detail.do?id=" + model.getId());
		
		if(Checkin.ON_DUTY.equals(model.getType())) {
			event.setCssClass("event-success");
			event.setTitle("上班考勤 " + getCheckinTime(model));
		} else {
			event.setCssClass("event-info");
			event.setTitle("下班考勤 " + getCheckinTime(model));
		}
		
//		event.setStart(model.getCreateDateTime().getTime());
		if(model.getLocateDate() != null) {
			event.setStart(model.getLocateDate().getTime());
			event.setEnd(model.getLocateDate().getTime());
		} else {
			event.setStart(model.getCreateDateTime().getTime());
			event.setEnd(event.getStart() + 1);
		}
		return event;
	}
	
	public static List<CheckinEvent> from(List<Checkin> models) {
		List<CheckinEvent> list = new ArrayList<CheckinEvent>();
		for(Checkin model : models) {
			list.add(from(model));
		}
		return list;
	}
	
	private static String getCheckinTime(Checkin model) {
		Date date = model.getLocateDate();
		if (date == null) {
			date = model.getCreateDateTime();
		}
		return TIME_FORMAT.format(date);
	}

	private String id;

	private String title;

	private String url;

	private String cssClass;

	private long start;

	private long end;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public long getStart() {
		return start;
	}

	public void setStart(long start) {
		this.start = start;
	}

	public long getEnd() {
		return end;
	}

	public void setEnd(long end) {
		this.end = end;
	}
}
